package cn.bdqn.controller;

import cn.bdqn.mapper.PersonMapper;
import cn.bdqn.model.Person;
import cn.bdqn.util.ExcelUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import static org.apache.commons.lang3.time.DateUtils.addDays;

/**
 * @author gurilla
 * @create 2019-08-11 10:05
 */
@RestController
public class EasyPoiTestController {
    @Resource
    private PersonMapper personMapper;

//    @RequiresPermissions("export")
    @RequestMapping("export")
    public void export(HttpServletResponse response){
        List<Person> personList = new ArrayList<>();
        //模拟从数据库获取需要导出的数据
       // List<Person> personList = personMapper.getall();

        Person person1 = new Person("1","路飞",1,new Date());
        Person person2 = new Person("2","娜美",2, addDays(new Date(),3));
        Person person3 = new Person("3","索隆",1, addDays(new Date(),10));
        Person person4 = new Person("4","小狸猫",1, addDays(new Date(),-10));
        personList.add(person1);
        personList.add(person2);
        personList.add(person3);
        personList.add(person4);


        //导出操作
        ExcelUtil.exportExcel(personList,"花名册","草帽一伙", Person.class,"海贼王.xls",response);
    }
    @PostMapping("excelImport")
    public void excelImport(@RequestParam("file") MultipartFile file) {
        List<Person> personList = null;
        personList = ExcelUtil.importExcel(file,1,1, Person.class);

        personMapper.delete();
        personMapper.insertBatch(personList);
        Map<String,Object> result =new HashMap<>();
        personMapper.check(result);
        Integer r= (Integer) result.get("reuslt");
        System.out.println(r);

//        System.out.println(personList);
//        personMapper.insertBatch(personList);
//        for(Person p:personList){
//            personMapper.insert(p.getName(),p.getSex(),p.getBirthday());
//        }
//        System.out.println(personList);
    }
}
